#include <stdio.h>
#include <stdlib.h>
#include <math.h> 
 
#include <crtdbg.h>
#include "list_work.h"


void kolvo(SList *list)
{
	SList *newlist;

	newlist = list;
	while (newlist->next != 0){
		newlist->kol_vo = 0;
		newlist = newlist->next;
	}
	free(newlist);

}

void prostue_chisla (SList *list, int n)
{
	int i, j;
	int *arr;
	
	
	arr = (int*)  malloc (n * sizeof(int));

	for(i = 1; i < n; i++)
		arr[i] = 1;

	for(i = 2; i < n; i ++){
		if (arr[i] == 1);
		for( j = i + i ; j < n; j = j + i){
			if (arr[j] == 1)
				arr[j] = 0;
		}
	}

	for (i = 2; i < n ; i ++){
		if (arr[i] == 1)
			list = slist_append(list, i);
	}

	free(arr);
	
	kolvo(list);
	
	_CrtDumpMemoryLeaks();
}


void main()
{


	 SList *list;
	 SList *list0;
	 SList *list2;
	 
	int i;
	int n;
	int chislo;


	chislo = 10;
	n = (int) chislo / 2 + 1;//sqrt((double) chislo);

		_CrtDumpMemoryLeaks();

	list = slist_create(0);
	prostue_chisla(list, n);
	free(list);
		_CrtDumpMemoryLeaks();

	list2 = list->next;
	free(list);
	list = list2;
	list0 = list2;


		_CrtDumpMemoryLeaks();


		while(1){
			if (chislo % list->data == 0){
				list->kol_vo ++;
				chislo = chislo / (int) list->data;

				if(chislo == 1) {
					slist_free(list->next);
					list->next = 0;
					break;
			}
			
				/*if (list->next != 0)
				list = list->next;
				else 
					break;*/
				
				
			}
			else
			{
				if (list->next != 0)
				list2 = list;
				list  = list->next;
				

				if (list2->kol_vo == 0)
					slist_remove(list0, list2->data);
				_CrtDumpMemoryLeaks();
			}
		}

		while (list0->next != NULL){
			printf("%d ------ %d \n", (int) list0->next->data, (int) list0->next->kol_vo);
			list0 = list0->next;
		}

	
		slist_free(list0);
		
	system("pause");
	_CrtDumpMemoryLeaks();
}

